#include <cassert>
#include <functional>
#include <vector>
#include <numeric>
#include <thread>
#include <iomanip>
#include <iostream>


/* Riemann computes the riemann sum for a given function f between
 a given 'a' and a given 'b' with a given subinterval length. This
 method is for threaded use, so the result can be accessed from different
 threads. To avoid race conditions, mutex and lock_guard are used.
*/
void riemann(std::pair<long double,
             std::mutex>& result, std::function<double(double)> f,
             double a, double b, double intervalLength)
{
    assert(b>=a);
    assert(intervalLength > 0);

    double xi = a;
    long double sum = 0.;

    int nIntervals = static_cast<int>((b-a)/intervalLength);

//This for loop iterates over the intervals. In each step the surface of the interval is added.
    for(int i = 1; i<=nIntervals; ++i)
    {
        sum += f(xi + intervalLength/2)*intervalLength;
        xi +=intervalLength;
    };
    std::lock_guard<std::mutex> l (result.second);
    result.first +=sum;
}
